<?php
class ManageBoothEditForm extends Form
{
	var $hotel = array();

    function ManageBoothEditForm()
    {
        Form::Form("ManageBoothForm");

        $id 		= Url::get("id");

        if($id)
        {
        	$this->hotel = Db_Booth::getBoothById($id);
        }

        if(!$this->hotel)
        {
        	Url::redirect_current();
        }

       /* //Check quyền
		if(!User::have_permit("hotel_edit"))
        {
        	//$hotel_ids = Db_Hotel::get_hotel_ids(User::id());

        	if(!Db_Booth::have_permit_manage($this->hotel, User::id()))
	        {
	        	Url::access_denied();
	        }
        }*/
        #############################################################################

        $this->link_css("style/paging.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_css("style/hotel.css");
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        $this->link_js("javascript/hotelform.js");

        $this->link_outside_js("http://maps.google.com/maps/api/js?sensor=false");
        $this->link_js("javascript/map/geocoder_tool.js");
        $this->link_css("style/map/geocoder_tool.css");
    }

    function on_submit()
    {
        $hotel['name'] 			= Url::getParam('hotel_name', '');
        mb_internal_encoding("UTF-8");

//        $hotel['name'] = mb_strtoupper(mb_substr($hotel['name'],0,1)) . mb_substr($hotel['name'],1);
        $hotel['slug'] = String::safe_title($hotel["name"]);
        $hotel['desc'] 			= Url::getParam('hotel_desc', '');
        $hotel['url'] 		= Url::getParam('hotel_website', '');
        $hotel['time_c'] 	= time();
        $hotel['status'] 		= (int)Url::get('hotel_status', 0);
        if (isset($_FILES["file_image"]) && $_FILES["file_image"]["size"] > 0) {
            $store_in = 'images/upload/booth/';
            $nameImg = md5($_FILES["file_image"]["name"]);
            move_uploaded_file($_FILES["file_image"]["tmp_name"], $store_in .$nameImg.'.jpg' );
            $hotel['icon'] =  $store_in .$nameImg.'.jpg';
        } else {
            unset($hotel['icon']);
        }
        if (isset($_FILES["file_image2"]) && $_FILES["file_image2"]["size"] > 0) {
            $store_in = 'images/upload/booth/';
            $nameImg2 = md5($_FILES["file_image2"]["name"]);
            move_uploaded_file($_FILES["file_image2"]["tmp_name"], $store_in .$nameImg2.'.jpg' );
            $hotel['img'] =  $store_in .$nameImg2.'.jpg';
        } else {
            unset($hotel['img']);
        }

        $valid = true;
        if($hotel['name']=='')
        {
            $valid = false;
            $this->setFormError("error_name","Tên nhà hàng không được để trống!");
        }

        if($hotel['desc']=='')
        {
            $valid = false;
            $this->setFormError("error_desc","Phần giới thiệu không được để trống!");
        }

        if($valid)
        {
            $id = Url::get("id");



            if(Db_Booth::update($hotel,$id))
            {
                //Thanh cong
                $this->setFormSucces("success","Save hành công!");
                $href = Url::get('href');
	            if($href)
	            {
	            	Url::redirect_url($href);
	            }
	            else
	            {
	            	Url::redirect_current();
	            }
            }
            else
            {
                $this->setFormError("message","Có lỗi xảy ra");
            }
        }
    }

    function draw()
    {
        global $display;
        $display->add("WEB_ROOT",WEB_ROOT);
        $display->add("WEB_DIR",WEB_DIR);

        $this->beginForm(true);

        $act 		= Url::get("act");
        $id 		= Url::get("id");

        $hotel 		= $this->hotel;




        $display->add('image_default', 		$hotel['icon']);
        $display->add('image_default2', 		$hotel['img']);

        $display->add("hotel_id",			$hotel['id']);

        $breaks = array("<br />", "<br>", "<br/>");
        $hotel['desc']= str_ireplace($breaks, "\r\n", $hotel['desc']);
        $hotel['name']= str_ireplace($breaks, "\r\n", $hotel['name']);
        $display->add("hotel_desc",			$hotel['desc']);
        $display->add("hotel_name",			stripslashes(Url::get('hotel_name',$hotel['name'])));



        $display->add("hotel_website",		stripslashes(Url::get('hotel_website',$hotel['url'])));







        $display->add('hotel_status', 			array(1=>"Hiện",2=>"Ẩn"));
        $display->add("status_id",				Url::get('hotel_status',$hotel['status']));



        $display->add("title","Sửa nhà hàng");
        $display->add("title_submit","Sửa");

        $display->add("message",$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));

        $display->output("HotelForm");

        $this->endForm();
    }

    function get_data_checkbox()
    {
        $features_checkbox_name = array();
        $features = array();
        $features_group = Db_Filter::getHotelFilersGroup();

        foreach($features_group as $value)
        {
            $id = $value['id'];
            $features_temp = Db_Filter::getHotelFilersByGroupId($id);

            foreach($features_temp as $v){
                $features[$id][$v['id']] = $v['name'];
            }

            $features_checkbox_name[$id] = "chb_features_".$id;
        }
        return array("features_group"=>$features_group,"features_checkbox_name"=>$features_checkbox_name,"features"=>$features);
    }
}
?>